
#include "musicscore.h"
#include <QFile>
#include <QDebug>

MusicScore::MusicScore(QWidget *parent)
    : QWidget{parent}
{
    notenum = 0;
    holdnum = 0;
    Snum = 0;
    beatnum = 0;
    Notelist = nullptr;
}

bool MusicScore::InitByFile(QString fileName)
{
    QFile file(fileName);//创建文件对象
    //if(!file.open(QIODevice::ReadOnly)){
      //  return false;//打开失败
    //}
    file.open(QIODevice::ReadOnly);
    //读取所有内容
    QByteArray arrALL = file.readAll();
    arrALL.replace("\r\n","\n");
    QList<QByteArray> lineList = arrALL.split('\n');
    Snum = lineList.size() - 1;//除去第一行

    for(int i = 0;i <= Snum;i++){
        QList<QByteArray> colList = lineList[i].split(',');
        if(i == 0){
            name = colList[0];
            level = colList[1].toInt();
            BPM = colList[2].toDouble();
            notenum = colList[3].toInt();
            holdnum = colList[4].toInt();
            Notelist = new Note[notenum];
            //这里以后加上hold的定义
        }
        else{//i>=1

            if(colList[0].toInt() == 1){//如果是note
                if(colList[2].toInt() == 0){//如果不双压
                    beatnum += colList[3].toDouble();
                }
                Notelist[i - 1].setNote(colList[1].toInt(),beatnum,BPM);
            }//文件顺序:类型、track、bool双压、duration
            //else if(coList[0].toInt() == 2) 后面实现hold
        }
    }

   //qDebug() << "...";
   file.close();
   return true;
}

void MusicScore::setparent(QGraphicsScene *Scene)
{
    for(int i = 0;i < notenum;i++){
        Notelist[i].setparent(Scene);
    }
}

